package com.ulic.customerManApplication.usermodule.action;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseAction;
import com.ulic.customerManApplication.commonmodule.action.OrganizationAction;
import com.ulic.customerManApplication.commonmodule.service.ActivieService;
import com.ulic.customerManApplication.commonmodule.service.LogService;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.usermodule.bo.User;
import com.ulic.customerManApplication.usermodule.service.UserService;
import com.ulic.um.client.UMContext;
import com.ulic.um.client.UMUser;
@Component("loginAction")
@Scope("prototype")
public class LoginAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	private UserService userServices;
	private ActivieService activieService;
	private LogService logService;
	private Logger logger = Logger.getLogger(LoginAction.class);
	@Override
	public String execute() throws Exception {
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest(); 
		User user = new User();
		try {
			user = userServices.findUserByPwd(username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String content = "";
		if(user == null) {
			content = "使用用户名：" + username + " 尝试登录失败，原因是：用户名或密码错误！";
		} else {
			if(activieService.isUserActivie(user.getUm_user_id())){
				content = "使用用户名：" + username + " 尝试登录成功！";
			}else{
				content = "使用用户名：" + username + " 尝试登录失败，原因是：用户没有启用！";
			}
		}
		request.put("message", content);
		logService.addLog(httpServletRequest.getRemoteAddr().toString(),new Date(),CONSTANT.LOGIN_LOG_TYPE,content);
		logger.debug(content);
		if(user==null){
			return "error";
		}else{
			if(!activieService.isUserActivie(user.getUm_user_id())){
				return "error";
			}
		}
		OrganizationAction.reportTree=null;
		session.clear();
		session.put("rolename", userServices.getUserRoleName(user.getUm_user_id()));
		session.put("rname", userServices.getUserRName(user.getUm_user_id()));
		session.put(CONSTANT.USER_SESSION_KEY, user);
		recache();
		
		return SUCCESS;
	}
	public String login() throws Exception{
		
		UMUser  user = UMContext.getCurrentUser();
		if(user==null){
			return "error";
		}
		User u = userServices.findUserByUserId(user.getUlicUMUserID());
		if(u==null){
			return "error";
		}
		if(!activieService.isUserActivie(u.getUm_user_id())){
			return "NORIGHT";
		}
		session.put("rolename", userServices.getUserRoleName(user.getUlicUMUserID()));
		session.put(CONSTANT.USER_SESSION_KEY, u);
		recache();
		return SUCCESS;
	}
	public String logout() throws Exception{
		User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
//		Adminitrator admin = (Adminitrator)session.get(CONSTANT.ADMIN_SESSION_KEY);
//		HttpServletRequest httpServletRequest = ServletActionContext. getRequest(); 
//		String content = user!=null?("用户名："+user.getUser_Name()+" 的用户退出登录！"):("超级管理员："+admin.getUsername()+"退出登录！");
//		logService.addLog(httpServletRequest.getRemoteAddr().toString(),new Date(),CONSTANT.LOGIN_LOG_TYPE,content);
//		logger.debug(content);
		ServletActionContext.getRequest().getSession().removeAttribute(CONSTANT.USER_SESSION_KEY);
		ServletActionContext.getRequest().getSession().removeAttribute(CONSTANT.ADMIN_SESSION_KEY);
		return user!=null?SUCCESS:"adminSuccess";
	}
	
	public String getUsername() {
		return username;
	}
	public String getPassword() {
		return password;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Resource
	public void setUserServices(UserService userServices) {
		this.userServices = userServices;
	}
	
	@Resource
	public void setLogService(LogService logService) {
		this.logService = logService;
	}
	@Resource
	public void setActivieService(ActivieService activieService) {
		this.activieService = activieService;
	}
	
	
}
